<?php


namespace App\Http\Controllers\Api\Order;


use App\Http\Controllers\Api\BasicController;
use App\Models\OrderAppoint;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\UserFocus;
use App\Models\UserInfo;
use App\Services\Sms;
use Carbon\Carbon;
use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Jiannei\Response\Laravel\Support\Facades\Response;
use Label84\HoursHelper\Facades\HoursHelper;

class MsgController extends BasicController
{

    /**
     * @api {post} /msg/check 预约&支付
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */

    public function check(){
        $uid=$this->uid();
        $redis=Redis::connection()->client();
        $valid_time = 86400-(time()+8*3600)%86400;
        $key='MSG_NUM:'.$uid.'_'.date('Ymd');

        if(!$redis->exists($key)) {
            $num = (int) config("base.free_msg_num");
            $redis->setex($key,$valid_time,$num);
            return Response::success($num);
        }

        $num=$redis->get($key);
        if($num > 0){
            $redis->decr($key);
            return Response::success(--$num);
        }
        return Response::fail('今日免费发送条数已用完');
    }


    public function start(){
        $oid=$this->request->input('oid');
        $order=OrderAppoint::where(['id'=>$oid,'status'=>2])->first();
        $order->begin_time=Carbon::now();
        $order->save();
        return Response::ok();
    }

    public function end(){
        $oid=$this->request->input('oid');
        $order=OrderAppoint::where(['id'=>$oid,'status'=>2])->first();
        $start = carbon::parse ($order->begin_time);
        $order->end_time=Carbon::now();
        $order->duration_time=(new Carbon())->diffInSeconds($start);
        $order->save();
        return Response::ok();
    }

}
